local jLootz = jLootz
local lib = jLootz:NewModule("Confirm")

function lib:SetArguments(text, OnValidate, ActivateChildren, ...)
	assert(not self.frame or not self.frame:IsShown())
	self.validate = assert(OnValidate)
	self.activate = assert(ActivateChildren)
	self.current_text = text

	local args = self.args
	if not args then
		args = {}
		self.args = args
	end
	for i = 1, select("#", ...) do
		args[i] = select(i, ...)
	end
	for i = #args, select("#", ...) + 1, -1 do
		args[i] = nil
	end
end

local function Frame_OnShow(self)
	lib.activate(false, unpack(lib.args))
end

local function Frame_OnHide(self)
	self.confirm:Enable()
	self.cancel:Enable()
	self.confirm:UnlockHighlight()
	self.cancel:UnlockHighlight()
	lib.activate(true, unpack(lib.args))
end

local function Confirm_Hide(self, confirm)
	local ff = LibStub("LibFrameFade-1.0")
	self.confirm:Disable()
	self.cancel:Disable()
	if confirm == true then
		self.confirm:LockHighlight()
	elseif confirm == false then
		self.cancel:LockHighlight()
	end
	ff.FrameFade(self, self.info)
end

local function Cancel_OnClick(self)
	Confirm_Hide(self:GetParent(), false)
end

local function Confirm_OnClick(self)
	lib.validate(unpack(lib.args))
	Confirm_Hide(self:GetParent(), true)
end

local function CreateTexture (parent, path, blend)
	local t = parent:CreateTexture()
	t:SetAllPoints(parent)
	t:SetTexture(path)
	if blend then
		t:SetBlendMode(blend)
	end
	return t
end

function lib:MakeFrame()
	local frame = CreateFrame("Frame", nil, UIParent)

	frame:SetScript("OnShow", Frame_OnShow)
	frame:SetScript("OnHide", Frame_OnHide)

	frame:SetWidth(128)
	frame:SetHeight(32)

	local bg = frame:CreateTexture(nil, "BACKGROUND")
	bg:SetAllPoints()
	bg:SetGradientAlpha("VERTICAL", 1, 1, 1, 0.2, 1, 1, 1, 0.01)

	local text = frame:CreateFontString(nil, "ARTWORK")

	text:SetFontObject(GameFontNormal)
	text:SetPoint("TOP")
	frame.text = text

	local confirm = CreateFrame("Button", nil, frame)

	confirm:SetNormalTexture(CreateTexture(confirm, "Interface\\RaidFrame\\ReadyCheck-Ready"))
	confirm:SetPushedTexture(CreateTexture(confirm, "Interface\\RaidFrame\\ReadyCheck-Ready"))
	confirm:SetHighlightTexture(CreateTexture(confirm, "Interface\\Buttons\\UI-Panel-MinimizeButton-Highlight", "ADD"))
	confirm:SetWidth(24)
	confirm:SetHeight(24)
	confirm:SetScript("OnClick", Confirm_OnClick)
	confirm:SetPoint("BOTTOMRIGHT")

	frame.confirm = confirm

	local cancel = CreateFrame("Button", nil, frame)

	cancel:SetNormalTexture(CreateTexture(cancel, "Interface\\RaidFrame\\ReadyCheck-NotReady"))
	cancel:SetPushedTexture(CreateTexture(cancel, "Interface\\RaidFrame\\ReadyCheck-NotReady"))
	cancel:SetHighlightTexture(CreateTexture(cancel, "Interface\\Buttons\\UI-Panel-MinimizeButton-Highlight", "ADD"))
	cancel:SetWidth(24)
	cancel:SetHeight(24)
	cancel:SetScript("OnClick", Cancel_OnClick)
	cancel:SetPoint("BOTTOMLEFT")

	frame.cancel = cancel

	frame.info = {
		timeToFade = .5,
		mode = "OUT",
		finishedFunc = function (self)
			self:Hide()
		end,
		finishedArg1 = self,
	}

	self.frame = frame
	self.MakeFrame = nil
	return frame
end

function lib:Show(parent)
	local frame = self.frame or self:MakeFrame()
	frame.text:SetText(self.current_text or "Confirm")
	frame:ClearAllPoints()
	frame:SetParent(parent)
	frame:SetFrameLevel(parent:GetFrameLevel() + 10)
	frame:SetPoint("CENTER", parent, "TOP")
	frame:Show()
end

function lib:Hide()
	local frame = self.frame
	if not frame then return end
	if frame:IsShown() then
		frame:Hide()
	end
	frame:SetParent(nil)
end
